package com.cisco.pso;

public class DiffResult {
	protected String raw;
	protected DiffType type;

	/**
	 * e.g. base/T27L10NSP25/trainingcenter/project.xml
	 */
	protected String leftFile;

	/**
	 * e.g. base/T28L10NSP8/trainingcenter/project.xml
	 */
	protected String rightFile;

	/**
	 * e.g. project.xml
	 */
	protected String shortFileName;

	public DiffResult(String line, String leftFolder, String rightFolder) {
		this.raw = line;
		type = DiffType.getType(line);
		String[] fileNames = type.extractFileNames(line, leftFolder,
				rightFolder);
		leftFile = fileNames[0];
		rightFile = fileNames[1];

		String fileName = leftFile != null ? leftFile : rightFile;
		int index = fileName.lastIndexOf('/');
		shortFileName = fileName.substring(index + 1);

	}

	public String getRaw() {
		return raw;
	}

	public void setRaw(String raw) {
		this.raw = raw;
	}

	public DiffType getType() {
		return type;
	}

	public void setType(DiffType type) {
		this.type = type;
	}

	public String getLeftFile() {
		return leftFile;
	}

	public void setLeftFile(String leftFile) {
		this.leftFile = leftFile;
	}

	public String getRightFile() {
		return rightFile;
	}

	public void setRightFile(String rightFile) {
		this.rightFile = rightFile;
	}

	public String getShortFileName() {
		return shortFileName;
	}

	public void setShortFileName(String shortFileName) {
		this.shortFileName = shortFileName;
	}

	public String getSubFolder(int depth) {
		String fileName = leftFile != null ? leftFile : rightFile;

		String path = fileName.substring(0,
				fileName.length() - shortFileName.length() );

		int lastIndexOfSlash = path.lastIndexOf('/');
		for (int i = 0; i < depth; i++)
			lastIndexOfSlash = path.lastIndexOf('/', lastIndexOfSlash-1);
		String subFolder = path.substring(lastIndexOfSlash+1);
		
		if(subFolder.equals("/"))
			subFolder="";
		
		return subFolder;
	}
	
	public String getModule() {
		String fileName = leftFile != null ? leftFile : rightFile;

		String module = fileName.split("/")[2];
		
		return module;
	}

	public static void main(String[] args) {
		String[] result;

		String line = "Files base/T27L10NSP25/trainingcenter/project.xml and base/T28L10NSP8/trainingcenter/project.xml differ";
		DiffResult dr = new DiffResult(line, "base/T27L10NSP25/",
				"base/T28L10NSP8/");
		System.out.println(dr.getLeftFile());
		System.out.println(dr.getRightFile());
		System.out.println(dr.getShortFileName());
		System.out.println(dr.getSubFolder(0));
		System.out.println(dr.getModule());

		System.out.println("==========");
		line = "Only in base/T28L10NSP8/webcomponents/src/java/com/webex/webapp/component/common/taglib: WebcompHtmlMessageTag.java";

		dr = new DiffResult(line, "base/T27L10NSP25/", "base/T28L10NSP8/");
		System.out.println(dr.getLeftFile());
		System.out.println(dr.getRightFile());
		System.out.println(dr.getShortFileName());
		System.out.println(dr.getSubFolder(1));
		System.out.println(dr.getModule());
	}
}
